﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModbusTCPLib
{
    /// <summary>
    /// BYteArray工具类，一般用来做报文拼接
    /// </summary>
    class ByteArray
    {
        #region 初始化
        private List<byte> list = new List<byte>();
        #endregion

        #region 属性
        /// <summary>
        /// List集合
        /// </summary>
        public List<byte> List
        {
            get { return list; }
        }
        /// <summary>
        /// Array数组
        /// </summary>
        public byte[] Array
        {
            get { return list.ToArray(); }
        }
        /// <summary>
        /// 长度
        /// </summary>
        public int Length
        {
            get { return list.Count; }
        }
        #endregion

        #region 方法
        /// <summary>
        /// 添加一个字节
        /// </summary>
        /// <param name="item"></param>
        public void Add(byte item)
        {
            list.Add(item);
        }

        /// <summary>
        /// 添加一个数组
        /// </summary>
        /// <param name="array"></param>
        public void Add(byte[] array)
        {
            list.AddRange(array);
        }

        /// <summary>
        /// 添加一个集合
        /// </summary>
        /// <param name="list"></param>
        public void Add(List<byte> list)
        {
            list.AddRange(list);
        }

        /// <summary>
        /// 连续添加两个字节
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        public void Add(byte item1,byte item2)
        {
            Add(new byte[] { item1, item2 });
        }

        /// <summary>
        /// 连续添加三个字节
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        public void Add(byte item1, byte item2,byte item3)
        {
            Add(new byte[] { item1, item2, item3 });
        }

        /// <summary>
        /// 连续添加四个字节
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        public void Add(byte item1, byte item2, byte item3, byte item4)
        {
            Add(new byte[] { item1, item2, item3, item4 });
        }

        /// <summary>
        /// 连续添加五个字节
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        public void Add(byte item1, byte item2, byte item3, byte item4, byte item5)
        {
            Add(new byte[] { item1, item2, item3, item4, item5 });
        }

        /// <summary>
        /// 添加一个ByteArray对象
        /// </summary>
        /// <param name="byteArray"></param>
        public void Add(ByteArray byteArray)
        {
            Add(byteArray.Array);
        }

        /// <summary>
        /// 添加一个Short类型
        /// </summary>
        /// <param name="value"></param>
        public void Add(short value)
        {
            Add((byte)(value >> 8));
            Add((byte)(value));
        }

        /// <summary>
        /// 添加一个UShort类型
        /// </summary>
        /// <param name="value"></param>
        public void Add(ushort value)
        {
            Add((byte)(value >> 8));
            Add((byte)(value));
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            list.Clear();
        }
        #endregion
    }
}
